package com.nh.orderAction;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class TestPrint implements Printable {
	Map<String, List<TempVo>> theMap;
	private String theTitle;
	private String remake;

	/**
	 * 
	 */
	public TestPrint(Map<String, List<TempVo>> map, String title, String theRemake) {
		super();
		theMap = map;
		theTitle = title;
		remake = theRemake;
		// TODO Auto-generated constructor stub
	}

	public TestPrint() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the theMap
	 */
	public Map<String, List<TempVo>> getTheMap() {
		return theMap;
	}

	/**
	 * @param theMap
	 *            the theMap to set
	 */
	public void setTheMap(Map<String, List<TempVo>> theMap) {
		this.theMap = theMap;
	}

	/**
	 * @return the remake
	 */
	public String getRemake() {
		return remake;
	}

	/**
	 * @param remake
	 *            the remake to set
	 */
	public void setRemake(String remake) {
		this.remake = remake;
	}

	/**
	 * @return the theTitle
	 */
	public String getTheTitle() {
		return theTitle;
	}

	/**
	 * @param theTitle
	 *            the theTitle to set
	 */
	public void setTheTitle(String theTitle) {
		this.theTitle = theTitle;
	}

	/**
	 * * @param Graphic指明打印的图形环境 * @param
	 * PageFormat指明打印页格式（页面大小以点为计量单位，1点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842点） * @param
	 * pageIndex指明页号
	 **/
	public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
		System.out.println("pageIndex=" + pageIndex);
		Component c = null;
		// print string
		// String str = "一楼/20号   人数 ";
		String str = theTitle;
		// 转换成Graphics2D
		Graphics2D g2 = (Graphics2D) gra;
		// 设置打印颜色为黑色
		g2.setColor(Color.red);
		// 打印起点坐标
		double x = pf.getImageableX();
		double y = pf.getImageableY();
		switch (pageIndex) {
		case 0:
			// 设置打印字体（字体名称、样式和点大小）（字体名称可以是物理或者逻辑名称）
			// Java平台所定义的五种字体系列：Serif、SansSerif、Monospaced、Dialog 和 DialogInput
			Font font = new Font("新宋体", Font.PLAIN, 10);
			g2.setFont(font); // 设置字体
			// BasicStroke bs_3=new BasicStroke(0.5f);
			float[] dash1 = { 2.0f };
			// 设置打印线的属性。
			// 1.线宽 2、3、不知道，4、空白的宽度，5、虚线的宽度，6、偏移量
			g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
			// g2.setStroke(bs_3);//设置线宽
			float heigth = font.getSize2D(); // 字体高度
			System.out.println("x=" + x);
			// -1- 用Graphics2D直接输出
			// 首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
			// g2.drawLine(10,10,200,300);
			Image src = Toolkit.getDefaultToolkit().getImage("D:\\ASSISTANTMANAGE\\res\\SFT1.jpg");
			g2.drawImage(src, (int) x, (int) y, c);
			int img_Height = 10;
			int img_width = src.getWidth(c);
			// System.out.println("img_Height="+img_Height+"img_width="+img_width
			// ) ;
			g2.drawString(str, (float) x, (float) y + 1 * heigth + img_Height); // 第一排
			if (StringUtils.isNotEmpty(remake)) {
				img_Height=img_Height+20;
				g2.drawString("["+remake+"]", (float) x, (float) y + 1 * heigth + img_Height); // 第一排
			}

			// 换行
			g2.drawLine((int) x, (int) (y + 1 * heigth + img_Height + 10), (int) x + 200, (int) (y + 1 * heigth + img_Height + 10));

			// 菜字体
			font = new Font("新宋体", Font.PLAIN, 14);
			g2.setFont(font); // 设置字体

			int xx = 10;
			int yy = 0;

			Iterator it = theMap.keySet().iterator();

			while (it.hasNext()) {
				font = new Font("新宋体", Font.PLAIN, 12);
				g2.setFont(font); // 设置字体
				String key = (String) it.next();
				List<TempVo> list = theMap.get(key);
				yy = yy + 30;
				g2.drawString("【" + key + "】", xx, yy + 1 * heigth + img_Height);
				for (int i = 0; i < list.size(); i++) {
					font = new Font("新宋体", Font.PLAIN, 12);
					g2.setFont(font); // 设置字体
					yy = yy + 15;
					TempVo tempVo = list.get(i);
					g2.drawString(tempVo.getTitle() + " (" + tempVo.getCout() + ")", xx, yy + 1 * heigth + img_Height);
					if (StringUtils.isNotEmpty(tempVo.getRemake())) {
						font = new Font("新宋体", Font.PLAIN, 8);
						g2.setFont(font); // 设置字体
						yy = yy + 10;
						g2.drawString("{备注：" + tempVo.getRemake() + "}", xx, yy + 1 * heigth + img_Height);

					}

				}
			}

			// for (int i = 0; i < 5; i++) {
			// yy = yy + 30;
			// g2.drawString("主食   XXXX菜    1份   【不要什么...】   ", xx, yy + 1 *
			// heigth + img_Height);
			// g2.drawString("主食   XXXX菜    1份   【不要什么...】   ", xx, yy + 1 *
			// heigth + img_Height);
			// }

			// g2.drawImage(src, (int) x, (int) (y + 1 * heigth + img_Height +
			// 11), c);
			return PAGE_EXISTS;
		default:
			return NO_SUCH_PAGE;
		}
	}

	public void test(Map<String, List<TempVo>> map, String title, String remake) {
		// ReadData();
		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		//p.setSize(590, 840); // 纸张大小(590, 840)表示A4纸
		p.setImageableArea(10, 10, 260, 2048); // A4(595 X
		// 842)设置打印区域，其实0，0应该是72，72
		// ，因为A4纸的默认X,Y边距是72

		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(new TestPrint(map, title, remake), pf);
		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();
		// 设置打印类
		job.setPageable(book);
		try {
			job.print();
			//job.print();
			/*
			 * // 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印 boolean a =
			 * job.printDialog(); if (a) { job.print(); }
			 */
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TestPrint test = new TestPrint();
		// test.test();
	}

}